/**
 * Copyright (C) 2013 SHANGHAI VOLKSWAGEN, All rights reserved.
 * License version 1.0, a copy of which has been included with this.
 *
 * @File name：com.market.cart.server.service.impl.AddrServiceImpl
 * @Create on：2024/3/9
 * @Author：liuchang
 */
package com.market.cart.server.service.impl;

import com.market.cart.common.domain.DTOAddr;
import com.market.cart.common.domain.DeliveryAddress;
import com.market.cart.common.domain.TDeliveryAddress;
import com.market.cart.server.mapper.AddrMapper;
import com.market.cart.server.service.AddrService;
import com.market.common.core.domain.Result;
import com.market.common.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>ClassName：AddrServiceImpl</p >
 * <p>Description：</p >
 * <p>Author：liuchang</p >
 * <p>Date：2024/3/9</p >
 */
@Service
public class AddrServiceImpl implements AddrService {
    @Autowired
    private AddrMapper addrMapper;
    @Autowired
    private TokenService tokenService;
    /**
     * <p>三级联动--递归</p >
     * @author：liuchang
     * @date：2024/3/9
     * @param：
     * @return：
    */
    @Override
    public Result findAllAddress() {
        List<DTOAddr> list = addrMapper.findAll ();

        List<DTOAddr> collect = list.stream ()
                .filter (dtoAddr -> {
                    //过滤条件pid==0
                    return dtoAddr.getAddrPid () == 0;
                })
                .map (dtoAddr -> {
                    dtoAddr.setChildren (getChildren (list, dtoAddr).isEmpty () ? null : getChildren (list, dtoAddr));
                    return dtoAddr;
                }).sorted (new Comparator<DTOAddr> () {
                    @Override
                    public int compare(DTOAddr o1, DTOAddr o2) {
                        return Integer.parseInt (o1.getAddrCode ()) - Integer.parseInt (o2.getAddrCode ());
                    }
                }).collect (Collectors.toList ());


        return Result.success (collect);
    }

    @Override
    public Result addAddress(DeliveryAddress deliveryAddress) {
        Long userid = tokenService.getLoginUser().getUserid();
        deliveryAddress.setUserId(Integer.valueOf(userid.intValue()));
        Integer i= addrMapper.addAddress(deliveryAddress);
        return Result.success(i);
    }
    /*
    * 查找地址详情
    * */
    @Override
    public Result findAddrByUserId() {
        Long userid = tokenService.getLoginUser().getUserid();
        List<DeliveryAddress> deliveryAddress=addrMapper.findAddrByUserId(Integer.valueOf(userid.intValue()));
        return Result.success(deliveryAddress);
    }
    /*
    * 新增地址
    * */
    private List<DTOAddr> getChildren(List<DTOAddr> list, DTOAddr dtoAddr) {
        List<DTOAddr> collect = list.stream ()
                .filter (dtoAddr1 -> {
                    //过滤条件子类的pid==父类的id
                    return Objects.equals (dtoAddr1.getAddrPid (), dtoAddr.getAddrId ());
                }).map (dtoAddr1 -> {
                    dtoAddr1.setChildren (getChildren (list, dtoAddr1).isEmpty () ? null : getChildren (list, dtoAddr1));
                    return dtoAddr1;
                }).sorted (new Comparator<DTOAddr> () {
                    @Override
                    public int compare(DTOAddr o1, DTOAddr o2) {
                        return Integer.parseInt (o1.getAddrCode ()) - Integer.parseInt (o2.getAddrCode ());
                    }
                }).collect (Collectors.toList ());

        return collect;
    }
}
